package itcast.dao.impl;

import itcast.dao.BuildmanagerDao;
import itcast.domain.Admin;
import itcast.domain.Buildmanager;
import itcast.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BuildmanagerDaoImpl implements BuildmanagerDao {
    /**
     * 申明JdbcTemplate类,出入连接池
     */
    private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<Buildmanager> findAll() {
        //使用jdbc操作数据库
        //1.定义sql
        String sql="select * from buildmanager";
        //query返回list集合，BeanPropertyRowMapper指定字节码
      List<Buildmanager>  buildmanagers=template.query(sql,new BeanPropertyRowMapper<Buildmanager>(Buildmanager.class));

      return buildmanagers;
    }

    @Override
    public void add(Buildmanager buildmanager) {
        //1.定义sql语句
        String sql="insert into buildmanager values(null,?,?,?,?,?)";
        //2.执行sql
        template.update(sql,buildmanager.getBuildmanager_username(),
                buildmanager.getBuildmanager_password(),
                buildmanager.getBuildmanager_name(),buildmanager.getBuildmanager_sex(),
                buildmanager.getBuildmanager_tel());
    }

    @Override
    public void delete(int id) {
        //1.定义sql
        String sql="delete from buildmanager where Buildmanager_ID=?";
        //2.执行sql
        template.update(sql,id);
    }

    //实现接口查询方法

    @Override
    public Buildmanager findById(int id) {
        //1.定义sql语句
        String sql="select * from buildmanager where Buildmanager_ID=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<Buildmanager>(Buildmanager.class),id);
    }

    //实现修改数据的方法

    @Override
    public void update(Buildmanager buildmanager) {
        //1.定义sql语句
        String sql="update buildmanager set Buildmanager_Username=?, Buildmanager_Password=?," +
                " Buildmanager_Name=?, Buildmanager_Sex=?, Buildmanager_Tel=? where  Buildmanager_ID=?";
        //执行sql语句
        template.update(sql,buildmanager.getBuildmanager_username(),buildmanager.getBuildmanager_password(),
                buildmanager.getBuildmanager_name(),buildmanager.getBuildmanager_sex(),
                buildmanager.getBuildmanager_tel(),buildmanager.getBuildmanager_id());
    }

    //查询总记录页
    @Override
    public int findTotalCount() {
        String sql="Select count(*) from buildmanager";

        return template.queryForObject(sql,Integer.class);
    }

    //查询list集合
    @Override
    public List<Buildmanager> findByPage(int start,int rows) {
        String sql="select * from buildmanager limit ? ,?";
        return template.query(sql,new BeanPropertyRowMapper<Buildmanager>(Buildmanager.class),start,rows);
    }
    //条件查询

    @Override
    public List<Buildmanager> findByDate(Map<String, String[]> condition) {
        //1.定义模板初始化sql
        String sql="select * from buildmanager where 1=1";
        StringBuilder sb=new StringBuilder(sql);
        //2.遍历map中value是否有值
        Set<String> keySet =condition.keySet();
        //定义参数集合
        List<Object> params=new ArrayList<Object>();
        for(String key :keySet){
            //获取value
          String value=condition.get(key)[0];
          //判断value是否有值
            if(value!=null && !"".equals(value)){
                sb.append(" and "+key+" like ? ");
                params.add("%"+value+"%");//条件的值
            }
        }
        System.out.println(sb.toString());
        System.out.println(params);
        List<Buildmanager> buildmanagers=template.query(sb.toString(),new BeanPropertyRowMapper<Buildmanager>(Buildmanager.class),params.toArray());
        return buildmanagers;
    }
    //楼宇管理员登录


  /*  @Override
    public Buildmanager denlu(String buildmanager_username, String buildmanager_password) {
        //使用jdbc操作数据库
        //1.定义Sql
        try {
            String sql="select * from buildmanager where Buildmanager_Username=? and Buildmanager_Password=?";
            Buildmanager buildmanager= template.queryForObject(sql,new BeanPropertyRowMapper<Buildmanager>(Buildmanager.class),buildmanager_username,buildmanager_password);
            return buildmanager;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

        }*/

    @Override
    public Buildmanager denglu(String buildmanager_username, String buildmanager_password) {
        //使用jdbc操作数据库
        //1.定义Sql
        try {
            String sql="select * from buildmanager where Buildmanager_Username=? and Buildmanager_Password=?";
            Buildmanager buildmanager= template.queryForObject(sql,new BeanPropertyRowMapper<Buildmanager>(Buildmanager.class),buildmanager_username,buildmanager_password);
            return buildmanager;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }
}
